ActiveReports 9 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Page Report/RDL Report Walkthroughs > Advanced > Custom Resource Locator |
Page reports can get resources from your file system using file paths, but sometimes resources are preserved in very specific sources, such as a database. With page reports, you can create a custom resource locator to read any resources that might be required by your reports from any location. This walkthrough is based on the Custom Resource Locator sample and illustrates how to load pictures from the user's My Pictures directory.
This walkthrough is split into the following procedures:
Note: Although this walkthrough uses Page reports, but this can be implemented using RDL reports also. |
When you complete this walkthrough you get a layout that looks similar to the following at runtime.
Property Name | Property Value |
---|---|
Name | Image1 |
Location | 0.1in, 0.1in |
Size | 2.8in, 2.8in |
Value | MyPictures:Penguins.jpg |
Property Name | Property Value |
---|---|
Name | Image2 |
Location | 3.1in, 0.1in |
Size | 2.8in, 2.8in |
Value | MyPictures:Desert.jpg |
To write the code in Visual Basic.NET
VB code. Paste on TOP |
Copy Code
|
---|---|
Imports System Imports System.Drawing Imports GrapeCity.ActiveReports.Extensibility Imports System.Globalization Imports System.IO Imports System.Runtime.InteropServices |
VB code. Paste INSIDE the class |
Copy Code
|
---|---|
Inherits ResourceLocator Private Const UriSchemeMyImages As String = "MyPictures:" ' Obtain and return the resource. Public Overrides Function GetResource(resourceInfo As ResourceInfo) As Resource Dim name As String = resourceInfo.Name If name Is Nothing OrElse name.Length = 0 Then Throw New ArgumentException("The name of resource to be obtained should be non-empty string.", "name") End If Dim uri As New Uri(name) Dim stream As Stream = GetPictureFromSpecialFolder(name) If stream Is Nothing Then stream = New MemoryStream() End If Return New Resource(stream, uri) End Function ' Returns the specified image from Public Pictures folder. Private Shared Function GetPictureFromSpecialFolder(path As String) As Stream Dim startPathPos As Integer = UriSchemeMyImages.Length If startPathPos >= path.Length Then Return Nothing End If Dim pictureName As String = path.Substring(startPathPos) Dim myPicturesPath As String = Environment.GetEnvironmentVariable("public") & "\Pictures\Sample Pictures" If Not myPicturesPath.EndsWith("\") Then myPicturesPath += "\" End If Dim picturePath As String = System.IO.Path.Combine(myPicturesPath, pictureName) If Not File.Exists(picturePath) Then Return Nothing End If Dim stream As New MemoryStream() Try Dim picture As Image = Image.FromFile(picturePath) picture.Save(stream, picture.RawFormat) stream.Position = 0 Catch generatedExceptionName As OutOfMemoryException ' The file is not valid image, or GDI+ doesn't support such images. Return Nothing Catch generatedExceptionName As ExternalException Return Nothing End Try Return stream End Function |
To write the code in C#
C# code. Paste on TOP |
Copy Code
|
---|---|
using System; using System.Drawing; using System.Globalization; using System.IO; using System.Runtime.InteropServices; using System.Windows.Forms; using GrapeCity.ActiveReports.Extensibility; using your_project_name.Properties; |
C# code. Paste BELOW the Using statements |
Copy Code
|
---|---|
namespace your_project_name { // Look for the resources in My Pictures folder. internal sealed class MyPicturesLocator : ResourceLocator { private const string UriSchemeMyImages = "MyPictures:"; // Obtain and return the resource. public override Resource GetResource(ResourceInfo resourceInfo) { string name = resourceInfo.Name; if (name == null || name.Length == 0) { throw new ArgumentException("The name of resource to be obtained should be non-empty string.", "name"); } Uri uri = new Uri(name); Stream stream = GetPictureFromSpecialFolder(name); if (stream == null) { stream = new MemoryStream(); } return new Resource(stream, uri); } // Returns the specified image from Public Pictures folder. private static Stream GetPictureFromSpecialFolder(string path) { int startPathPos = UriSchemeMyImages.Length; if (startPathPos >= path.ToString().Length) { return null; } string pictureName = path.ToString().Substring(startPathPos); string myPicturesPath = Environment.GetEnvironmentVariable("public") + "\\Pictures\\Sample Pictures"; if (!myPicturesPath.EndsWith("\\")) myPicturesPath += "\\"; string picturePath = Path.Combine(myPicturesPath, pictureName); if (!File.Exists(picturePath)) return null; MemoryStream stream = new MemoryStream(); try { Image picture = Image.FromFile(picturePath); picture.Save(stream, picture.RawFormat); stream.Position = 0; } catch (OutOfMemoryException) // The file is not valid image, or GDI+ doesn't support such images. { return null; } catch (ExternalException) { return null; } return stream; } } } |
Property Name | Property Value |
---|---|
Name | PreviewForm |
Text | Preview Form |
Size | 1015, 770 |
Property Name | Property Value |
---|---|
Name | reportPreview1 |
Dock | Fill |
To write the code in Visual Basic.NET
VB code. Paste BELOW the Import statements |
Copy Code
|
---|---|
Imports GrapeCity.ActiveReports.Document Imports System.IO Imports GrapeCity.ActiveReports |
VB code. Paste INSIDE the Load event |
Copy Code
|
---|---|
Dim reportData As Stream = [GetType]().Assembly.GetManifestResourceStream("your_project_name.DemoReport.rdlx") reportData.Position = 0 Dim reader As New StreamReader(reportData) Dim def As New PageReport(reader) def.ResourceLocator = New MyPicturesLocator() Dim runtime As New PageDocument(def) reportPreview1.ReportViewer.LoadDocument(runtime) |
To write the code in C#
C# code. Paste BELOW the Using statements |
Copy Code
|
---|---|
using GrapeCity.ActiveReports.Document; using System.IO; using GrapeCity.ActiveReports; |
C# code. Paste INSIDE the Load event |
Copy Code
|
---|---|
string myPicturesPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); Stream reportData = GetType().Assembly.GetManifestResourceStream("your_project_name.DemoReport.rdlx"); reportData.Position = 0; StreamReader reader = new StreamReader(reportData); PageReport def = new PageReport(reader); def.ResourceLocator = new MyPicturesLocator(); PageDocument runtime = new PageDocument(def); reportPreview1.ReportViewer.LoadDocument(runtime); |